package com.lxj.fast_android_demo.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 数据集对象
 * 
 * @author LiangXiangJu
 * @creationTime 2014-6-30 上午10:54:22
 * @version
 */
public class Dataset {
	// 行游标
	private int cursor = -1;
	// 列域(属性和属性列定义一一对应)
	private HashMap<String, DataField> fields = new HashMap<String, DataField>();
	// 行记录
	private ArrayList<DataRecord> records = new ArrayList<DataRecord>();

	public Dataset(List<HashMap<String, String>> fieldDatas) {
		if (null != fieldDatas) {
			for (HashMap<String, String> fieldData : fieldDatas) {
				DataField dataField = new DataField(this, fieldData);
				addField(dataField);
			}
		}
	}

	/**
	 * 记录条数
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:33:24
	 * @version
	 * @return
	 */
	public int recordCount() {
		return records.size();
	}

	/**
	 * 验证属性是否存在
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-2 下午3:24:54
	 * @version
	 * @param fieldName
	 * @return
	 */
	public boolean fieldValid(String fieldName) {
		return fields.containsKey(fieldName);
	}

	/**
	 * 获取列定义
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:34:55
	 * @version
	 * @param fieldName
	 * @return
	 */
	public DataField getField(String fieldName) {
		if (fieldValid(fieldName)) {
			return fields.get(fieldName);
		}
		return null;
	}

	/**
	 * 新增行记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:35:09
	 * @version
	 */
	public void addRecord() {
		addRecord(null);
	}

	/**
	 * 新增行记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:35:32
	 * @version
	 * @param datas
	 */
	public void addRecord(HashMap<String, String> datas) {
		DataRecord record = new DataRecord(this, datas);
		records.add(record);
		goLast();
	}

	/**
	 * 删除行记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:35:45
	 * @version
	 * @return
	 */
	public boolean deleteRecord() {
		if (isLegal(cursor)) {
			int temIndex = cursor;
			if (cursor == recordCount() - 1) {
				cursor--;
			}
			records.remove(temIndex);
			return true;
		}
		return false;
	}

	/**
	 * 删除指定行标的记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:39:08
	 * @version
	 * @param index
	 * @return
	 */
	public boolean deleteRecordAt(int index) {
		if (isLegal(index)) {
			cursor = index;
			return deleteRecord();
		}
		return false;
	}

	/**
	 * 获取当前行记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:39:52
	 * @version
	 * @return
	 */
	public DataRecord getRecord() {
		if (isLegal(cursor)) {
			return records.get(cursor);
		}
		return null;
	}

	/**
	 * 获取指定行标的记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:40:23
	 * @version
	 * @param index
	 * @return
	 */
	public DataRecord getRecordAt(int index) {
		if (isLegal(index)) {
			cursor = index;
			return getRecord();
		}
		return null;
	}

	/**
	 * 根据列名获取当前记录的列值对象
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:41:06
	 * @version
	 * @param fieldName
	 * @return
	 */
	public Object getData(String fieldName) {
		DataRecord record = getRecord();
		if (null != record) {
			return record.getData(fieldName);
		}
		return null;
	}

	/**
	 * 根据列名获取当前记录的列值文本
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:41:53
	 * @version
	 * @param fieldName
	 * @return
	 */
	public String getValue(String fieldName) {
		Object fieldData = getData(fieldName);
		if (null != fieldData) {
			return String.valueOf(fieldData);
		}
		return null;
	}

	/**
	 * 为当前行的某列设置列值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:42:24
	 * @version
	 * @param value
	 * @param fieldName
	 */
	public void setDataForField(Object data, String fieldName) {
		DataRecord record = getRecord();
		if (null != record) {
			record.setDataForField(data, fieldName);
		}
	}

	/**
	 * 为当前行的某列设置列值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-2 下午3:37:28
	 * @version
	 * @param value
	 * @param fieldName
	 */
	public void setValueForField(String value, String fieldName) {
		DataRecord record = getRecord();
		if (null != record) {
			record.setValueForField(value, fieldName);
		}
	}

	/**
	 * 游标复位至头部(第一行之前的位置)
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:43:12
	 * @version
	 */
	public void goBeforeFirst() {
		cursor = -1;
	}

	/**
	 * 移动游标至前一行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:44:35
	 * @version
	 * @return
	 */
	public boolean goPrevious() {
		return goBackwardWithStep_(1);
	}

	/**
	 * 移动游标至下一行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:45:25
	 * @version
	 * @return
	 */
	public boolean goNext() {
		return goForwardWithStep_(1);
	}

	/**
	 * 游标后退指定行数
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:45:39
	 * @version
	 * @param step
	 * @return
	 */
	public boolean goBackwardWithStep_(int step) {
		return goToIndex_(cursor - step);
	}

	/**
	 * 游标前进指定行数
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:46:55
	 * @version
	 * @param step
	 * @return
	 */
	public boolean goForwardWithStep_(int step) {
		return goToIndex_(cursor + step);
	}

	/**
	 * 移动游标至第一行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:47:56
	 * @version
	 * @return
	 */
	public boolean goFirst() {
		return goToIndex_(0);
	}

	/**
	 * 移动游标至最后行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:48:24
	 * @version
	 * @return
	 */
	public boolean goLast() {
		return goToIndex_(recordCount() - 1);
	}

	/**
	 * 移动游标至指定行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:48:49
	 * @version
	 * @param index
	 * @return
	 */
	public boolean goToIndex_(int index) {
		if (isLegal(index)) {
			cursor = index;
			return true;
		}
		return false;
	}

	/**
	 * 行记录是否为空
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:49:03
	 * @version
	 * @return
	 */
	public boolean isEmpty() {
		return 0 == records.size();
	}

	/**
	 * 游标是否复位至头部
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:49:32
	 * @version
	 * @return
	 */
	public boolean isBeforeFirst() {
		return -1 == cursor;
	}

	/**
	 * 游标是否在第一行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:50:05
	 * @version
	 * @return
	 */
	public boolean isFirst() {
		return 0 == cursor;
	}

	/**
	 * 游标是否处于最后行
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:50:40
	 * @version
	 * @return
	 */
	public boolean isLast() {
		if (isLegal(cursor) && cursor == recordCount() - 1) {
			return true;
		}
		return false;
	}

	/**
	 * 获取游标值
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:51:33
	 * @version
	 * @return
	 */
	public int getCursor() {
		return cursor;
	}

	/**
	 * 获取所有列
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-7-2 下午3:40:36
	 * @version
	 * @return
	 */
	public HashMap<String, DataField> getFields() {
		return fields;
	}

	/**
	 * 获取所有记录
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:52:44
	 * @version
	 * @return
	 */
	public ArrayList<DataRecord> getRecords() {
		return records;
	}

	/**
	 * 新增列定义
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:34:31
	 * @version
	 * @param dataField
	 */
	private void addField(DataField dataField) {
		// 有行记录后不再更新fields
		if (records.size() > 0) {
			return;
		}
		if (null != dataField) {
			// fields.add(dataField);
			fields.put(dataField.getName(), dataField);
		}
	}

	/**
	 * 指定行数是否合法
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-30 上午10:51:16
	 * @version
	 * @param index
	 * @return
	 */
	private boolean isLegal(int index) {
		if (index >= 0 && index < recordCount()) {
			return true;
		}
		return false;
	}

}
